package com.zhixi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhixi.pojo.Dish;
import com.zhixi.pojo.DishFlavor;
import com.zhixi.pojo.SetmealDish;
import com.zhixi.service.DishService;
import com.zhixi.service.SetmealDishService;
import com.zhixi.mapper.SetmealDishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhangzhixi
 * @description 针对表【setmeal_dish(套餐菜品关系)】的数据库操作Service实现
 * @createDate 2022-07-13 11:35:33
 */
@Service
public class SetmealDishServiceImpl extends ServiceImpl<SetmealDishMapper, SetmealDish>
        implements SetmealDishService {

    @Autowired
    private DishService dishService;

    @Override
    public List<SetmealDish> getSetmealDishList(Long setMealId) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        // 1、查询到套餐下面的所有菜品数据
        queryWrapper.eq(SetmealDish::getSetmealId, setMealId);

        List<SetmealDish> setmealDishList = this.list(queryWrapper);
        // 2、处理菜品的图片数据
        setmealDishList = setmealDishList.stream().peek(item -> {
            Dish dish = dishService.getOne(new LambdaQueryWrapper<Dish>().eq(Dish::getId, item.getDishId()));
            if (dish != null) {
                item.setImage(dish.getImage());
            }
        }).collect(Collectors.toList());

        return setmealDishList;
    }
}




